package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sjlh.centurylianhua.entity.Inventory;
import com.sjlh.centurylianhua.entity.Sale;
import com.sjlh.centurylianhua.entity.SaleList;
import com.sjlh.centurylianhua.mapper.InventoryMapper;
import com.sjlh.centurylianhua.mapper.SaleListMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/saleList")
public class SaleListController {
    @Resource
    private SaleListMapper saleListMapper;
    @Resource
    private InventoryMapper inventoryMapper;
    /**
     * 添加销售日志详情
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody SaleList saleList){
        int r=saleListMapper.insert(saleList);
        if (r==1){
            UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("depotNum =  depotNum - " + saleList.getSlistNumber());//销售数量
            updateWrapper.eq("productId", saleList.getProductId());//商品id
            updateWrapper.eq("depotId", saleList.getRemarks1());//仓库id
            inventoryMapper.update(null, updateWrapper);//修改库存表
            return new ResultData(true,20000,"添加成功！",true,null);
        }else {
            return new ResultData(true,20000,"添加失败！",false,null);
        }

    }
    /**
     * 根据销售日志id查询进货日志详情
     * */
    @RequestMapping("/getAllSale")
    public  ResultData getAll(@RequestBody SaleList saleList){
        QueryWrapper<SaleList> wrapper = new QueryWrapper<>();
        wrapper.eq("saleId",saleList.getSaleId());
        //追加多表连接的sql语句
        wrapper.apply("product.id = salelist.productId");
        //调用mapper添加的多表连接方法查询销售日志详情
        List<SaleList> list=saleListMapper.getInfo(wrapper);
        long count=saleListMapper.getInfo(wrapper).size();
        ResultData rs=new ResultData(true,20000,"成功",list,count);
        return rs;
    }
}
